import store from '@/store'
import { SystemSetting } from '@/config'

/**
 * 验证用户是否具备某权限
 * @param {String|Array} value 权限标识字符串或数组
 * @returns {Boolean}
 */
export function checkPerm(value) {
  if (Array.isArray(value)) return hasPermOr(value)
  return hasPerm(value)
}

/**
 * 验证用户是否具备某角色
 * @param {String|Array} value 角色字符串或数组
 * @returns {Boolean}
 */
export function checkRole(value) {
  if (Array.isArray(value)) return hasRoleOr(value)
  return hasRole(value)
}


// 验证用户是否具备某权限
export function hasPerm(permission) {
  return authPermission(permission)
}

// 验证用户是否含有指定权限，只需包含其中一个
export function hasPermOr(permissions) {
  return permissions.some(item => authPermission(item))
}

// 验证用户是否含有指定权限，必须全部拥有
export function hasPermAnd(permissions) {
  return permissions.every(item => authPermission(item))
}

// 验证用户是否具备某角色
export function hasRole(role) {
  return authRole(role)
}

// 验证用户是否含有指定角色，只需包含其中一个
export function hasRoleOr(roles) {
  return roles.some(item => authRole(item))
}

// 验证用户是否含有指定角色，必须全部拥有
export function hasRoleAnd(roles) {
  return roles.every(item => authRole(item))
}



export function authPermission(permission) {
  const all_permission = SystemSetting.ALL_PERMISSION
  const permissions = store.getters && store.getters.permissions
  if (permissions && permissions.length > 0) {
    return permissions.some(v => all_permission === v || v === permission)
  } else {
    return false
  }
}

export function authRole(role) {
  const super_admin = SystemSetting.SUPER_ADMIN_ROLE
  const roles = store.getters && store.getters.roles
  if (roles && roles.length > 0) {
    return roles.some(v => super_admin === v || v === role)
  } else {
    return false
  }
}
